<template>
  <h1>循环指令</h1>
  <ul><li v-for="elements in arr">{{ elements }}</li>
  </ul>
  <ol>
    <li v-for="item in 5">{{ item }}</li>
  </ol>

  <table border="1px">
    <caption>商品列表</caption>
    <tr>
      <th>序列</th>
      <th>商品名称</th>
      <th>商品价格</th>
      <th>商品库存</th>
    </tr>
    <tr v-for="(item,index) in productArr">
      <td>{{ index + 1 }}</td>
      <td>{{ item.title }}</td>
      <td>{{ item.price }}</td>
      <td>{{ item.num }}</td>
      <td>
        <button @click="del(index)">删除</button>
      </td>
    </tr>
  </table>
</template>

<script setup>
import {ref} from "vue";

const arr = ref(['元素1', '元素2', '元素3', '元素4', '元素5']);

const productArr = ref([
  {title: '面包', price: 20, num: 68},
  {title: '苹果', price: 30, num: 12},
  {title: '香蕉', price: 40, num: 34},
  {title: '摩托车', price: 50000, num: 3}
]);

const del = (index) => {
  if (confirm('是否删除?')) {
   productArr.value.splice(index, 1);
  }
}
</script>

<style scoped>

</style>